from django.db import models

# Create your models here.
class UserInfo(models.Model):
    name = models.CharField(verbose_name='用户名',max_length=32)
    password = models.CharField(verbose_name='密码',max_length=32)
    age = models.IntegerField(verbose_name='年龄',null=False,default=20)
    sex = models.CharField(verbose_name='性别',max_length=32,choices=(('male','男'),('female','女')),
                           null=True,blank=True)

    class Meta:
        db_table = 'test_user'
        verbose_name = '用户信息表' #影响数据库
        verbose_name_plural='用户信息表' #admin后台显示支持
        ordering = ('-id',)  # 排序 降序


class Author(models.Model):
    author_name = models.CharField(max_length=64, null=False)


class AuthorDetail(models.Model):
    author_sex = models.IntegerField(choices=((0, "男"), (1, "女")), null=False)
    author_phone = models.CharField(max_length=11, null=False)
    author_age = models.IntegerField(null=False)
    # 一对一关系的两个类，关系模型写到哪个类里都行，要顾及代码执行顺序，要写到下面的类里
    # models.CASCADE是级联删除，即关系方数据删掉，本表的相关信息也一并删掉
    author = models.OneToOneField(to=Author, to_field="id", on_delete=models.SET_NULL, blank=True, null=True)

#出版社
class Publisher(models.Model):
    publisher_name  = models.CharField(max_length=30,null=False)
    publisher_address = models.CharField(max_length=50,null=False)
    publisher_city = models.CharField(max_length=30,null=False)
    publisher_website = models.URLField(null=True)

class Book(models.Model):
    book_name = models.CharField(max_length=20,null=False)
    #一对多关系，要写到多的类里
    publisher = models.ForeignKey(to=Publisher,to_field="id",on_delete=models.CASCADE)
    price  = models.FloatField(max_length=6,null=None)
    # 多对多关系与一对一关系同理，写到哪个都行，也是顾及代码执行顺序，要写到下面的类里
    author = models.ManyToManyField(to=Author)


class Subject(models.Model):
    name = models.CharField(max_length=64)
    remark = models.CharField(max_length=256, blank=True, null=True)

    class Meta:
        #managed = False
        db_table = 'subject'